package com.core.family.admin.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.core.family.admin.domain.CodePltEnum;
import jdk.nashorn.internal.runtime.URIUtils;
import jodd.http.HttpUtil;
import jodd.util.StringUtil;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

import com.core.family.admin.mapper.OpencodeMapper;
import com.core.family.admin.domain.Opencode;
import com.core.family.admin.service.OpencodeService;
import com.core.common.gen.BaseServiceImpl;
import org.springframework.web.util.UriUtils;


/**
 * @author honglei
 * @version 1.0
 * @time: 2024-5-8 14:41:10
 * @Description 服务实现层 开源项目表
 */
@Slf4j
@Service
public class OpencodeServiceImpl extends BaseServiceImpl<OpencodeMapper, Opencode> implements OpencodeService {


    @SneakyThrows
    @Override
    public Boolean simple(String sourcePath, String mark, boolean isUpdate) {
        if (StringUtil.isEmpty(sourcePath)) {
            return false;
        }
        Opencode opencode = getOpencode(sourcePath);
        opencode.setMark(mark);
        opencode.setUrl(sourcePath);
        if (isUpdate) {
            Opencode dbOpen = this.baseMapper.selectOne(new QueryWrapper<Opencode>().eq("url", sourcePath));
            if (dbOpen != null) {
                opencode.setId(dbOpen.getId());
                if (! StringUtil.isEmpty(dbOpen.getMark())){
                    if (!dbOpen.getMark().contains(mark)){
                        opencode.setMark(dbOpen.getMark() + "," + mark);
                    }else {
                        opencode.setMark(null);
                    }
                }

            }
        }
        return saveOrUpdate(opencode);
    }

    private Opencode getOpencode(String sourcePath) {
        Opencode opencode = new Opencode();
        Document document = null;
        String dis = null, license = null, mlanguage = null, name;
        String names[] = sourcePath.split("/");
        if (names.length > 0) {
            name = names[names.length - 1].replace(".git", "");
            opencode.setName(name);
        }
        //gitee
        if (CodePltEnum.GITEE.getCode() == CodePltEnum.selectCode(sourcePath)) {
            StringBuilder projectLabel = new StringBuilder();
            try {
                document = Jsoup.connect(sourcePath).get();
                dis = document.title(); // 获取网页标题
                license = null == document.getElementById("license-popup")
                        ? null : document.getElementById("license-popup").text();
                mlanguage = null == document.selectFirst(".summary-languages")
                        ? null : document.selectFirst(".summary-languages").text().split("等")[0];
                Elements ets = document.select(".project-label-item");
                ets.forEach(et -> {
                    projectLabel.append(et.text())
                            .append(";");
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
            opencode.setLicenseType(license)
                    .setDis(StringUtil.isEmpty(dis) ? null : dis)
                    .setLabel(StringUtil.isEmpty(projectLabel) ? null : projectLabel.toString())
                    .setCodeLang(StringUtil.isEmpty(mlanguage) ? null : mlanguage)
                    .setPlatform(CodePltEnum.GITEE.getCode());
        }
        //github
        if (CodePltEnum.GITHUB.getCode() == CodePltEnum.selectCode(sourcePath)) {
            // 发送HTTP GET请求并获取网页内容
            StringBuilder projectLabel = new StringBuilder();
            try {
                document = Jsoup.connect(sourcePath).get();
                dis = null == document.selectFirst("p.f4.mb-3")
                        ? null : document.selectFirst("p.f4.mb-3").text();
                license = null == document.selectFirst("div.mb-2 a.Link--muted")
                        ? null : document.selectFirst("div.mb-2 a.Link--muted").text();
                mlanguage = null == document.selectFirst("div.mb-2 span.Progress span")
                        ? null : document.selectFirst("div.mb-2 span.Progress span").attr("aria-label");
                Elements ets = document.select("div.my-3 div.f6 a");
                ets.forEach(et -> {
                    projectLabel.append(et.text())
                            .append(";");
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
            opencode.setLicenseType(license)
                    .setDis(StringUtil.isEmpty(dis) ? null : dis)
                    .setLabel(StringUtil.isEmpty(projectLabel) ? null : projectLabel.toString())
                    .setCodeLang(StringUtil.isEmpty(mlanguage) ? null : mlanguage)
                    .setPlatform(CodePltEnum.GITHUB.getCode());
        }
        return opencode;
    }


}

